package com.zyl.ag.ui.home;

import android.app.Activity;
import android.app.AlertDialog;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.zyl.ag.R;
import com.zyl.ag.SysApplication;
import com.zyl.ag.bean.AgTD;
import com.zyl.ag.ui.widget.WidgetProvider;
import com.zyl.util.SharedPreferencesUtil;
import com.zyl.util.UrlUtil;

public class HomeActivity  extends Activity {

	final Context context = this;

	private int mAppWidgetId;
	private Button okButton;
	private EditText editText1, editText2;
	private  TextView currentPrice;
	TextView mTitleView;



	public void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		setContentView(R.layout.home_activity);
		mTitleView = (TextView) findViewById(R.id.title_text);
		mTitleView.setText("首页");
 
 
		currentPrice = (TextView) findViewById(R.id.currentPrice);
		
		 String str=UrlUtil.getAgTD();
		 AgTD ag=AgTD.getAgTD(str);  
		currentPrice.setText(ag.toString());
		editText1 = (EditText) findViewById(R.id.editText1);
		editText2 = (EditText) findViewById(R.id.editText2);
		okButton = (Button) findViewById(R.id.ok_Button);
		Bundle localBundle = getIntent().getExtras();
		if (localBundle != null)
			this.mAppWidgetId = localBundle.getInt("appWidgetId", 0);
		this.editText1.setText(SharedPreferencesUtil.getHigh(this)+"");
		this.editText2.setText(SharedPreferencesUtil.getLow(this)+"");
		okButton.setOnClickListener(okClickListener);
	}
	 OnClickListener okClickListener=new OnClickListener() {
		public void onClick(View arg0) {
			String text = "";
			SharedPreferencesUtil.saveSetting(context, editText1.getText().toString(), editText2
					.getText().toString());

			AppWidgetManager appWidgetManager = AppWidgetManager
					.getInstance(context);
			WidgetProvider.updateTextAppWidget(context,
					appWidgetManager, text);
			Intent resultValue = new Intent();
			resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
					mAppWidgetId);
			setResult(RESULT_OK, resultValue);
			finish();
		}
	};
	
	
	public void refresh(View arg0) {
		String str = UrlUtil.getAgTD();
		AgTD ag = AgTD.getAgTD(str);
		currentPrice.setText(ag.toString());
		Toast.makeText(context, "已刷新", Toast.LENGTH_SHORT);
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		 
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
		 
			 ShowExitdialog() ;
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	protected void ShowExitdialog() 
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(HomeActivity.this);

		builder.setMessage("确定要退出吗?");

		builder.setTitle("提示");

		builder.setPositiveButton("确认",
				new android.content.DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
					 
			            
						dialog.dismiss();
						 SysApplication.getInstance().exit();
						//HomeActivity.this.finish();
					}

				});

		builder.setNegativeButton("取消",
				new android.content.DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {

						dialog.dismiss();

					}

				});

		builder.create().show();

	}
}
